/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-common
*
* @File name : WeChatPushMessageUtil.java
*
* @Author : LiuJun
*
* @Date : 2017年1月5日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年1月5日    LiuJun    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package org.marketing.common.util;

import java.util.List;

import org.marketing.common.constants.BusinessConstant;
import org.marketing.common.constants.MessagePushConstant;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocol;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolMessageContent;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolMessageTemplateContent;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolReceiver;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolSender;
import org.marketing.common.exception.UtilException;

/**
*
* @author LiuJun
* 微信推送消息工具类
* @date 2017年1月5日
*/
public class WeChatPushMessageUtil {
    
    /**
    *
    * @author LiuJun
    * 获取微信模板消息队列对象
    * @date 2017年1月5日
    * @param templateType-模板类型
    * @param receivesList-模板消息接收人员
    * @param contentList-模板内容列表
    * @param authorizerAppid-授权方appid-公众号appId-可从cookies中获取
    * @return
    */
    public static WeChatMessageProtocol getWeChatTemplate(String templateType, 
                                                          List<String> receivesList,
                                                          List<WeChatMessageProtocolMessageTemplateContent> contentList,
                                                          String authorizerAppid,String dealerCode) throws UtilException {
        if (CheckUtil.NullOrEmpty(templateType) || CheckUtil.NullOrEmpty(receivesList)
            || CheckUtil.NullOrEmpty(contentList) || CheckUtil.NullOrEmpty(authorizerAppid)
            || CheckUtil.NullOrEmpty(dealerCode)) {
            throw new UtilException("the indispensable params be likely to empty");
        }
        try {
            WeChatMessageProtocol weChatMessageProtocol = new WeChatMessageProtocol();

            WeChatMessageProtocolReceiver receiver = new WeChatMessageProtocolReceiver();
            receiver.setDetail(true);
            receiver.setPlatform(MessagePushConstant.PUSH_PLATFORM.WC_PUBLIC);
            //设置模板消息接收人员
            receiver.addDetailList(receivesList);
            WeChatMessageProtocolMessageContent message = new WeChatMessageProtocolMessageContent();
            //设置模板消息类别
            message.setContent(templateType);
            message.setSourceType(BusinessConstant.SOURCE_TYPE_WECHAT);
            //设置模板类型
            for(WeChatMessageProtocolMessageTemplateContent content:contentList){
                message.addTemplateConentList(content);
            }

            WeChatMessageProtocolSender sender = new WeChatMessageProtocolSender();
            sender.setOriginApplicationHost("host1");
            sender.setOriginApplicationName("saas-wechat");
            sender.setClientAppId(authorizerAppid);
            sender.setDealerCode(dealerCode);
            weChatMessageProtocol.setMessage(message);
            weChatMessageProtocol.setReceiver(receiver);
            weChatMessageProtocol.setSender(sender);
            weChatMessageProtocol.setPushChannel(MessagePushConstant.PUSH_CHANNEL.WEBCHAT);

            return weChatMessageProtocol;
        } catch (Exception e) {
            throw new UtilException(e.getMessage(), e);
        }
    }

}
